package com.luck.easyfoodbatch.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * Created by CMY on 2016/5/12.
 */
public class TimeUtils {

    private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;
    private static final long ONE_WEEK = 604800000L;

    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "月前";
    private static final String ONE_YEAR_AGO = "年前";

    /**
     * 格式化时间段，以当前时间为间断，分可选和不可选
     */
    public static List<HashMap<String, Object>> initTime() {
        List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        String time = String.valueOf(System.currentTimeMillis()).substring(0, 10);
        String s = SubTime2(time);
        long toLong = getTimeToLong(s);
        HashMap<String, Object> map;
        for (int i = 0; i < 24; i++) {
            map = new HashMap<String, Object>();
            switch (i) {
                case 0:
                    if (toLong < getTimeToLong("9:00")) {
                        map.put("time", "9:00-9:30");
                        map.put("isChecked", true);
                    } else {
                        map.put("time", "9:00-9:30");
                        map.put("isChecked", false);
                    }
                    break;
                case 1:
                    if (toLong > getTimeToLong("9:30")) {
                        map.put("time", "9:30-10:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "10:00-10:30");
                        map.put("isChecked", true);
                    }
                    break;
                case 2:
                    if (toLong > getTimeToLong("10:00")) {
                        map.put("time", "10:00-10:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "10:30-11:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 3:
                    if (toLong > getTimeToLong("10:30")) {
                        map.put("time", "10:30-11:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "11:00-11:30");
                        map.put("isChecked", true);
                    }
                    break;
                case 4:
                    if (toLong > getTimeToLong("11:00")) {
                        map.put("time", "11:00-11:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "11:30-12:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 5:
                    if (toLong > getTimeToLong("11:30")) {
                        map.put("time", "11:30-12:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "12:00-12:30");
                        map.put("isChecked", true);
                    }

                    break;
                case 6:
                    if (toLong > getTimeToLong("12:00")) {
                        map.put("time", "12:00-12:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "12:30-13:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 7:
                    if (toLong > getTimeToLong("12:30")) {
                        map.put("time", "12:30-13:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "13:00-13:30");
                        map.put("isChecked", true);
                    }
                    break;
                case 8:
                    if (toLong > getTimeToLong("13:00")) {
                        map.put("time", "13:00-13:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "13:30-14:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 9:
                    if (toLong > getTimeToLong("13:30")) {
                        map.put("time", "13:30-14:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "14:00-14:30");
                        map.put("isChecked", true);
                    }
                    break;
                case 10:
                    if (toLong > getTimeToLong("14:00")) {
                        map.put("time", "14:00-14:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "14:30-15:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 11:
                    if (toLong > getTimeToLong("14:30")) {
                        map.put("time", "14:30-15:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "15:00-15:30");
                        map.put("isChecked", true);
                    }
                    break;
                case 12:
                    if (toLong > getTimeToLong("15:00")) {
                        map.put("time", "15:00-15:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "15:30-16:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 13:
                    if (toLong > getTimeToLong("15:30")) {
                        map.put("time", "15:30-16:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "16:00-16:30");
                        map.put("isChecked", true);
                    }
                    break;
                case 14:
                    if (toLong > getTimeToLong("16:00")) {
                        map.put("time", "16:00-16:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "16:30-17:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 15:
                    if (toLong > getTimeToLong("16:30")) {
                        map.put("time", "16:30-17:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "17:00-17:30");
                        map.put("isChecked", true);
                    }
                    break;
                case 16:
                    if (toLong > getTimeToLong("17:00")) {
                        map.put("time", "17:00-17:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "17:30-18:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 17:
                    if (toLong > getTimeToLong("17:30")) {
                        map.put("time", "17:30-18:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "18:00-18:30");
                        map.put("isChecked", true);
                    }
                    break;
                case 18:
                    if (toLong > getTimeToLong("18:00")) {
                        map.put("time", "18:00-18:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "18:30-19:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 19:
                    if (toLong > getTimeToLong("18:30")) {
                        map.put("time", "18:30-19:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "19:00-19:30");
                        map.put("isChecked", true);
                    }
                    break;
                case 20:
                    if (toLong > getTimeToLong("19:00")) {
                        map.put("time", "19:00-19:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "19:30-20:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 21:
                    if (toLong > getTimeToLong("19:30")) {
                        map.put("time", "19:30-20:00");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "20:00-20:30");
                        map.put("isChecked", true);
                    }
                    break;
                case 22:
                    if (toLong > getTimeToLong("20:00")) {
                        map.put("time", "20:00-20:30");
                        map.put("isChecked", false);
                    } else {
                        map.put("time", "20:30-21:00");
                        map.put("isChecked", true);
                    }
                    break;
                case 23:
                    if (toLong > getTimeToLong("20:30")) {
                        map.put("time", "20:30-21:00");
                        map.put("isChecked", false);
                    }
                    break;

            }
            list.add(map);
        }
        return list;
    }

    /**
     * 获取当天+count天的日期
     *
     * @param date
     * @param count
     * @return
     */
    public static Date getNextDay(Date date, int count) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, count);
        date = calendar.getTime();
        return date;
    }

    /**
     * 显示纯汉字的星期名称
     *
     * @param i 星期：1,2,3,4,5,6,7
     * @return
     * @author TangWei 2013-10-25上午11:31:51
     */
    public static String changeWeekToHanzi(int i) {
        switch (i) {
            case 1:
                return "星期一";
            case 2:
                return "星期二";
            case 3:
                return "星期三";
            case 4:
                return "星期四";
            case 5:
                return "星期五";
            case 6:
                return "星期六";
            case 7:
                return "星期日";
            default:
                return "";
        }
    }

    /**
     * 截取时间段的前一个时间
     *
     * @param time
     */
    public static String subTimeString(String time) {
        String subTime = "";
        time.trim();
        if (time.startsWith("9")) {
            subTime = time.substring(0, 4);
        } else {
            subTime = time.substring(0, 5);
        }
        return subTime;
    }

    /**
     * 将某个时间转成时间戳
     *
     * @param time
     * @return
     */
    public static String getStringToGetTime(String time) {
        String substring = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date d = sdf.parse(time);
            substring = String.valueOf(d.getTime() / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return substring;
    }

    /**
     * 将某个时间转成时间戳
     *
     * @param time
     * @return
     */
    public static String getStringToGetTime2(String time) {
        String substring = "";
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd hh:mm");
        try {
            Date d = sdf.parse(time);
            substring = String.valueOf(d.getTime() / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return substring;
    }

    /**
     * 将某个时间转成时间戳
     *
     * @param time
     * @return
     */
    public static String getStringToGetLongTime(String time) {
        String substring = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d = sdf.parse(time);
            substring = String.valueOf(d.getTime() / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return substring;
    }

    public static long getStringTimeToLong(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        return date.getTime() / 1000;
    }

    public static long getTimeToLong(String time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        return date.getTime() / 1000;
    }

    public static String SubTime(String time) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Long of = Long.valueOf(time);
        str = sdf.format(new Date(of * 1000));
        return str;
    }

    public static String SubTime2(String time) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Long of = Long.valueOf(time);
        str = sdf.format(new Date(of * 1000));
        return str;
    }

    public static String format(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date d = format.parse(date);
            long delta = new Date().getTime() - d.getTime();
            if (delta < 1L * ONE_MINUTE) {
                long seconds = toSeconds(delta);
                return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
            }
            if (delta < 45L * ONE_MINUTE) {
                long minutes = toMinutes(delta);
                return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
            }
            if (delta < 24L * ONE_HOUR) {
                long hours = toHours(delta);
                return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
            }
            if (delta < 48L * ONE_HOUR) {
                return "昨天";
            }
            if (delta < 30L * ONE_DAY) {
                long days = toDays(delta);
                return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
            }
            if (delta < 12L * 4L * ONE_WEEK) {
                long months = toMonths(delta);
                return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
            } else {
                long years = toYears(delta);
                return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }

    private static long toSeconds(long date) {
        return date / 1000L;
    }

    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }

    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }

    /**
     * 将时间戳转为代表"距现在多久之前"的字符串
     *
     * @param timeStr 时间戳
     * @return
     */
    public static String getStandardDate(String timeStr) {

        StringBuffer sb = new StringBuffer();

        long t = Long.parseLong(timeStr);
        long time = System.currentTimeMillis() - (t * 1000);
        long mill = (long) Math.ceil(time / 1000);// 秒前

        long minute = (long) Math.ceil(time / 60 / 1000.0f);// 分钟前

        long hour = (long) Math.ceil(time / 60 / 60 / 1000.0f);// 小时

        long day = (long) Math.ceil(time / 24 / 60 / 60 / 1000.0f);// 天前

        if (day - 1 > 0) {
            if (day <= 7) {
                sb.append(day + "天");
            }
        } else if (hour - 1 > 0) {
            if (hour >= 24) {
                sb.append("1天");
            } else {
                sb.append(hour + "小时");
            }
        } else if (minute - 1 > 0) {
            if (minute == 60) {
                sb.append("1小时");
            } else {
                sb.append(minute + "分钟");
            }
        } else if (mill - 1 > 0) {
            if (mill == 60) {
                sb.append("1分钟");
            } else {
                sb.append(mill + "秒");
            }
        } else {
            sb.append("刚刚");
        }
        if (!sb.toString().equals("刚刚")) {
            if (day > 7) {
                sb.append(getTimeToLong(timeStr));
            } else {
                sb.append("前");
            }
        }
        return sb.toString();
    }

    /**
     * 获取当前的时间戳
     *
     * @param time
     * @return
     */
    public static String getStringToDate(String time) {
        Date date = null;
        String substring = null;
        try {
            date = new Date();
            long l = date.getTime();
            String str = String.valueOf(l);
            substring = str.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return substring;
    }

    public static String TimeToStr(String time) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Long of = Long.valueOf(time);
        str = sdf.format(new Date(of * 1000));
        return str;
    }

    public static String TimeToStrReset(String time) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Long of = Long.valueOf(time);
        str = sdf.format(new Date(of * 1000));
        return str;
    }

    public static String TimeToStr2(String time) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        Long of = Long.valueOf(time);
        str = sdf.format(new Date(of * 1000));
        return str;
    }

    /**
     * 获取当前时间的小时和分钟数
     *
     * @return
     */
    public static String[] toTime() {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        double minute = c.get(Calendar.MINUTE);
        minute = minute / 10;
        int mte = (int) (Math.round(minute) * 10);
        c.set(year, month, date, hour, mte + 20);
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);
        int m = (int) minute;
        return new String[]{String.valueOf(hour), String.valueOf(m)};
    }

    public static String[] toHour() {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        c.set(year, month, date, hour + 1, minute);
        hour = c.get(Calendar.HOUR_OF_DAY);
        return new String[]{String.valueOf(hour)};
    }

    public static String SubTime3(String time) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Long of = Long.valueOf(time);
        str = sdf.format(new Date(of * 1000));
        return str;
    }

    public static String SubTime4(String time) {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        Long of = Long.valueOf(time);
        str = sdf.format(new Date(of * 1000));
        return str;
    }

    /**
     * 将 1800 加个":",变成 18:00
     *
     * @param str
     * @return
     */
    public static String addColon(String str) {
        if (str == null || str.length() != 4) {
            return null;
        }
        return str.substring(0, 2) + ":" + str.substring(2, 4);
    }

    /**
     * 格式化日期，针对于传过来的日期是毫秒数
     *
     * @param date   日期毫秒数
     * @param format 格式化样式 示例：yyyy-MM-dd HH:mm:ss
     * @return
     * @author TangWei 2013-11-29上午11:31:49
     */
    public static String formatDate(Object date, String format) {
        try {
            return new SimpleDateFormat(format).format(new Date(Long
                    .parseLong(Tools.formatString(date)) * 1000));
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 格式化日期，针对于传过来的日期是毫秒数<br>
     * 转换样式：2013-11-12 11:12:13
     *
     * @param date 日期毫秒数
     * @return
     * @author TangWei 2013-11-22上午11:38:13
     */
    public static String formatTime(Object date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String subFormatDate(Object date) {
        String data = Tools.formatString(date);
        try {
            return data.split(" ")[0];
        } catch (Exception e) {
            return "";
        }
    }

    public static String formatTime(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    /**
     * 掉此方法输入所要转换的时间输入例如（"2014-06-14-16-09-00"）返回时间戳
     *
     * @param time
     * @return
     */
    public static String formatDataToTimeStamp(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdr.parse(time);
            long l = date.getTime();
            String stf = String.valueOf(l);
            times = stf.substring(0, 10);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return times;
    }

    /**
     * 格式化日期，针对于传过来的日期是毫秒数<br>
     * 转换样式：2013-11-12
     *
     * @param date 日期毫秒数
     * @return
     * @author TangWei 2013-11-22上午11:38:13
     */
    public static String formatDate(Object date) {
        return formatDate(date, "yyyy-MM-dd");
    }


}
